/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.core.base;

import org.proteusframework.core.api.model.IDisplayableName;
import org.proteusframework.core.api.model.INamespace;
import org.proteusframework.core.util.Assert;

import java.net.URL;
import java.util.logging.Logger;

/**
 * Fully qualified name that includes two additional properties, name and description. Displayable names are used to in
 * a UI to present the user with more meaningful names.
 *
 * @author Tacoma Four
 */
public class DisplayableName extends Namespace implements IDisplayableName
{
    /**
     * Logger.
     */
    private static final Logger logger = Logger.getLogger(DisplayableName.class.getCanonicalName());

    /**
     * Human readable name.
     */
    private String name;

    /**
     * Human readable description.
     */
    private String description;

    /**
     * Glyph URL used to visually represent the object.
     */
    private URL glyphURL;

    /**
     * Constructs a displayable name by extracting the values out of the given displayable name object. This constructor
     * is useful for cast protection. Instead of passing in a <code>this</code> reference, construct a new
     * <code>DisplayableName</code> with this constructor, guaranteeing that the recipient of the displayable name
     * cannot try to cast the object to its physical implementation.
     *
     * @param displayableName Cast protected displayable name
     */
    public DisplayableName(final IDisplayableName displayableName)
    {
        super(displayableName);

        this.name = displayableName.getName();
        this.description = displayableName.getDescription();
        this.glyphURL = displayableName.getGlyphURL();
    }

    /**
     * Defines an immutable displayable name with a default icon.
     *
     * @param namespace   Namespace, typically reverse domain notation
     * @param id          Unique ID within the given namespace
     * @param name        Human readable name
     * @param description Human readable description. If a null is passed, it is converted to an empty-string.
     * @throws IllegalArgumentException when namespace, id, or name is null
     */
    public DisplayableName(final String namespace, final String id, final String name, final String description)
            throws IllegalArgumentException
    {
        this(namespace, id, null, name, description, (URL) null);
    }

    /**
     * Defines an immutable displayable name with a default icon.
     *
     * @param namespace   Namespace, typically reverse domain notation
     * @param id          Unique ID within the given namespace
     * @param refId       Reference ID
     * @param name        Human readable name
     * @param description Human readable description. If a null is passed, it is converted to an empty-string.
     * @throws IllegalArgumentException when namespace, id, or name is null
     */
    public DisplayableName(final String namespace, final String id, String refId, final String name, final String description)
            throws IllegalArgumentException
    {
        this(namespace, id, refId, name, description, (URL) null);
    }


    /**
     * Defines an immutable displayable name based strictly on a namespace.
     *
     * @param namespace Non-null namespace object
     */
    public DisplayableName(final INamespace namespace)
    {
        this(namespace.getFamily(), namespace.getId(), namespace.getRefId(), namespace.getId(), namespace.getId() + " object", (URL) null);
    }

    /**
     * Defines an immutable displayable name with a default icon.
     *
     * @param namespace   Namespace, typically reverse domain notation
     * @param id          Unique ID within the given namespace
     * @param refId       Reference ID
     * @param name        Human readable name
     * @param description Human readable description. If a null is passed, it is converted to an empty-string.
     * @param glyphURL    Fully qualified path in the form of a URL to the image to use when graphically
     *                    representing this object
     * @throws IllegalArgumentException when namespace, id, or name is null
     */
    public DisplayableName(final String namespace,
                           final String id,
                           final String refId,
                           final String name,
                           final String description,
                           final URL glyphURL)
            throws IllegalArgumentException
    {
        super(namespace, id, refId);
        Assert.parameterNotNull(name, "Parameter 'name' must not be null");

        this.name = name;
        this.description = (null == description)
                ? ""
                : description;

        this.glyphURL = (null == glyphURL)
                ? assignMissingIcon()
                : glyphURL;
    }

    /**
     * Assigns the missing icon.
     *
     * @return MISSING_ICON URL
     */
    private URL assignMissingIcon()
    {
        return this.getClass().getResource(MISSING_ICON);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String getDescription()
    {
        return description;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final URL getGlyphURL()
    {
        return glyphURL;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String getName()
    {
        return name;
    }

    @Override
    public String toString()
    {
        return "DisplayableName{" +
                "namespace='" + getFamily() + '\'' +
                ", id='" + getId() + '\'' +
                ", refId='" + getRefId() + '\'' +
                ", name='" + name + '\'' +
                ", glyphURL='" + glyphURL.toString() + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}
